package com.geovis.emergency.spd.controller.system;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.file.dto.FileDTO;
import com.geovis.common.file.dto.UploadDTO;
import com.geovis.common.file.handler.IFileHandler;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.system.service.ISystemFileService;
import com.geovis.emergency.spd.entity.system.entity.SystemFile;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemFileDTO;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemFileQueryDTO;
import com.geovis.emergency.spd.entity.system.pojo.dto.SystemFileUploadDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 附件表 前端控制器
 * </p>
 *
 * @author 曾德实
 * @since 2020-09-01
 */
@RestController
@RequestMapping("/system/systemFile")
@AllArgsConstructor
@Api(value = "系统管理模块-附件相关接口", tags = "系统管理模块-附件相关接口")
@Slf4j
@Validated
public class SystemFileController extends BaseController<ISystemFileService> {

    private final IFileHandler fileHandler;

    @ApiOperation(value = "检查分片是否存在", notes = "检查分片是否存在")
    @GetMapping("/upload")
    public Result<UploadDTO> checkChunk(UploadDTO dto) {
        return Result.ok(dto);
    }

    @ApiOperation(value = "附件上传", notes = "附件上传")
    @PostMapping(value = "/upload")
    public Result<SystemFile> upload(SystemFileUploadDTO uploadDTO) {
        SystemFile systemFile = baseService.upload(uploadDTO);
        return Result.ok(systemFile);
    }

    @ApiOperation(value = "分页查询附件列表", notes = "分页查询附件列表")
    @PostMapping(value = "/listPage")
    public Result<PageResult<SystemFile>> listPage(@RequestBody @Validated PageParam<SystemFileQueryDTO> pageParam) {
        return Result.ok(baseService.listPage(pageParam));
    }

    @ApiOperation(value = "查询附件列表", notes = "查询附件列表")
    @PostMapping("/list")
    public Result<List<SystemFileDTO>> list(@RequestBody @Validated SystemFileQueryDTO queryDTO) {
        return Result.ok(baseService.getList(queryDTO));
    }

    @ApiOperation(value = "批量删除文件数据", notes = "批量删除文件数据")
    @ApiImplicitParams({@ApiImplicitParam(name = "idList", value = "删除id的list", required = true, dataType = "java.util.Set", paramType = "body")})
    @PostMapping("/removeByIdList")
    public Result<Set<String>> removeByIdList(@NotNull(message = "删除的id集合不能为空") @RequestBody Set<String> idList) {
        baseService.removeByIdList(idList);
        return Result.ok(idList);
    }

    @ApiOperation(value = "下载项目本地模板附件", notes = "通过文件名称下载项目本地模板附件")
    @GetMapping(value = "/downloadLocalFile")
    public ResponseEntity<byte[]> downloadLocalFile(@NotNull(message = "文件名不能为空") String fileName, String localFileDir) {
        return fileHandler.downloadLocalFile(fileName, localFileDir);
    }

    @ApiOperation(value = "查询附件下载", notes = "查询附件下载")
    @GetMapping(value = "/download")
    public ResponseEntity<byte[]> download(@RequestParam("idList") String[] idList) {
        List<SystemFile> list = baseService.listByIds(CollUtil.toList(idList));
        if (list.size() == 1) {
            FileDTO fileDTO = BeanUtil.copyProperties(list.get(0), FileDTO.class);
            return fileHandler.download(fileDTO);
        }
        List<FileDTO> zipList = list.stream().map(systemFile -> BeanUtil.copyProperties(systemFile, FileDTO.class)).collect(Collectors.toList());
        return fileHandler.zip(zipList);
    }

    @ApiOperation(value = "输出流", notes = "输出流")
    @ApiImplicitParam(name = "id", value = "附件id", required = true, dataType = "java.lang.String")
    @GetMapping(value = "/downloadOut/{id}")
    public ResponseEntity<byte[]> out(@PathVariable("id") String id) {
        SystemFile systemFile = baseService.getById(id);
        FileDTO fileDTO = BeanUtil.copyProperties(systemFile, FileDTO.class);
        return fileHandler.out(fileDTO);
    }

}
